/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.oskis.library.db;
import java.sql.*;
/**
 * Aus der Tabelle ssno eine neue ID lesen und diese in der Datenbank 1 hochsetzen.
 * liefert -1 im Fehlerfall
 * Anmerkung: könnte Sinn machen, eine SNONotFoundException auszulösen oder ähnlich
 * @author thomas
 */
public class SNO {
    private Connection db;
    private int newsno;
    
    public SNO(Connection db,String sno) throws SNONotFoundException {
        this.db=db;
        newsno=-1;
        Statement st;
        ResultSet rs;
        String SQL;
        try {
            db.setAutoCommit(false);
            st=db.createStatement();
            SQL="select value from ssno where sno='"+sno+"'";
            rs=st.executeQuery(SQL);
            if (rs.next()) {
                newsno=rs.getInt("value");
            } else {
                db.rollback();
                st.close();
                throw new SNONotFoundException("ID "+sno+" not found");
            }
            SQL="update ssno set value=value+1 where sno='"+sno+"'";
            st.execute(SQL);
            db.commit();
            db.setAutoCommit(true);
            st.close();
        } catch (SQLException e) {
            throw new SNONotFoundException("ID "+sno+" not found: SQL Error was: "+e.toString());            
        }
    }
    
    public int getNewSNO() {
        return(newsno);
    }
}
